package cn.shujuhai.cms.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.templateresolver.FileTemplateResolver;

/**
 * @Author: zhangDeZhong
 * @Date: 2024/12/5 12:01
 * @Description: 模板引擎配置
 **/
@Configuration
public class TemplateEngineConfig {

    @Value("${template.static.thymeleafUrl}")
    private String thymeleafUrl;

    public TemplateEngine getTemplateEngine() {
        FileTemplateResolver templateResolver = new FileTemplateResolver();

        // 动态设置模板文件路径
        templateResolver.setPrefix(thymeleafUrl);  // 设置外部目录路径
        templateResolver.setSuffix(".html");  // 设置文件后缀
        templateResolver.setTemplateMode("HTML");  // 设置模板模式
        templateResolver.setCharacterEncoding("UTF-8");  // 设置字符编码

        // 创建一个新的 TemplateEngine 实例，并设置新的 TemplateResolver
        TemplateEngine dynamicTemplateEngine = new TemplateEngine();
        dynamicTemplateEngine.setTemplateResolver(templateResolver);
        return dynamicTemplateEngine;
    }
}
